package org.jeecg.zhensi.customer.service.impl;

import org.jeecg.zhensi.customer.entity.BdCustomer;
import org.jeecg.zhensi.customer.entity.BdCustomerT;
import org.jeecg.zhensi.customer.entity.BdCustomerB;
import org.jeecg.zhensi.customer.entity.BdCustomerK;
import org.jeecg.zhensi.customer.mapper.BdCustomerTMapper;
import org.jeecg.zhensi.customer.mapper.BdCustomerBMapper;
import org.jeecg.zhensi.customer.mapper.BdCustomerKMapper;
import org.jeecg.zhensi.customer.mapper.BdCustomerMapper;
import org.jeecg.zhensi.customer.service.IBdCustomerService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 客户基本信息
 * @Author: jeecg-boot
 * @Date:   2022-06-21
 * @Version: V1.0
 */
@Service
public class BdCustomerServiceImpl extends ServiceImpl<BdCustomerMapper, BdCustomer> implements IBdCustomerService {

	@Autowired
	private BdCustomerMapper bdCustomerMapper;
	@Autowired
	private BdCustomerTMapper bdCustomerTMapper;
	@Autowired
	private BdCustomerBMapper bdCustomerBMapper;
	@Autowired
	private BdCustomerKMapper bdCustomerKMapper;
	
	@Override
	@Transactional
	public void saveMain(BdCustomer bdCustomer, List<BdCustomerT> bdCustomerTList,List<BdCustomerB> bdCustomerBList,List<BdCustomerK> bdCustomerKList) {
		bdCustomerMapper.insert(bdCustomer);
		if(bdCustomerTList!=null && bdCustomerTList.size()>0) {
			for(BdCustomerT entity:bdCustomerTList) {
				//外键设置
				entity.setPk_customer(bdCustomer.getId());
				bdCustomerTMapper.insert(entity);
			}
		}
		if(bdCustomerBList!=null && bdCustomerBList.size()>0) {
			for(BdCustomerB entity:bdCustomerBList) {
				//外键设置
				entity.setPk_customer(bdCustomer.getId());

				bdCustomerBMapper.insert(entity);
			}
		}
		if(bdCustomerKList!=null && bdCustomerKList.size()>0) {
			for(BdCustomerK entity:bdCustomerKList) {
				//外键设置
				entity.setPk_customer(bdCustomer.getId());
				bdCustomerKMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(BdCustomer bdCustomer,List<BdCustomerT> bdCustomerTList,List<BdCustomerB> bdCustomerBList,List<BdCustomerK> bdCustomerKList) {
		bdCustomerMapper.updateById(bdCustomer);
//
//		//1.先删除子表数据
//		bdCustomerTMapper.deleteByMainId(bdCustomer.getId());
//		bdCustomerBMapper.deleteByMainId(bdCustomer.getId());
//		bdCustomerKMapper.deleteByMainId(bdCustomer.getId());
//
//
//		//2.子表数据重新插入
//		if(bdCustomerTList!=null && bdCustomerTList.size()>0) {
//			for(BdCustomerT entity:bdCustomerTList) {
//				//外键设置
//				entity.setPk_customer(bdCustomer.getId());
//				//外键设置
//				bdCustomerTMapper.insert(entity);
//			}
//		}
//		if(bdCustomerBList!=null && bdCustomerBList.size()>0) {
//			for(BdCustomerB entity:bdCustomerBList) {
//				//外键设置
//				entity.setPk_customer(bdCustomer.getId());
//				//外键设置
//				bdCustomerBMapper.insert(entity);
//			}
//		}
//		if(bdCustomerKList!=null && bdCustomerKList.size()>0) {
//			for(BdCustomerK entity:bdCustomerKList) {
//				//外键设置
//				entity.setPk_customer(bdCustomer.getId());
//				bdCustomerKMapper.insert(entity);
//			}
//		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		bdCustomerTMapper.deleteByMainId(id);
		bdCustomerBMapper.deleteByMainId(id);
		bdCustomerKMapper.deleteByMainId(id);
		bdCustomerMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bdCustomerTMapper.deleteByMainId(id.toString());
			bdCustomerBMapper.deleteByMainId(id.toString());
			bdCustomerKMapper.deleteByMainId(id.toString());
			bdCustomerMapper.deleteById(id);
		}
	}
	
}
